<template>
  <transition name="toast-fade">
    <div v-if="visible" class="kiki-toast">
      {{ message }}
    </div>
  </transition>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';

export default defineComponent({
  name: 'ToastMessage',
  setup() {
    const visible = ref(false);
    const message = ref('');
    let timer: number | null = null;
    
    // 显示消息
    const show = (text: string, duration = 3000) => {
      // 清除之前的定时器
      if (timer) {
        clearTimeout(timer);
        timer = null;
      }
      
      // 设置消息并显示
      message.value = text;
      visible.value = true;
      
      // 设置自动隐藏
      timer = window.setTimeout(() => {
        visible.value = false;
        timer = null;
      }, duration);
    };

    // 导出 showToast 函数
    const showToast = (text: string, duration = 3000, isSuccess = true) => {
      show(text, duration);
    };
    
    return {
      visible,
      message,
      show,
      showToast
    };
  }
});
</script>

<style scoped>
.kiki-toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 10px 20px;
  border-radius: 4px;
  font-size: 14px;
  z-index: 2000;
  max-width: 80%;
  text-align: center;
}

.toast-fade-enter-active,
.toast-fade-leave-active {
  transition: opacity 0.3s, transform 0.3s;
}

.toast-fade-enter-from,
.toast-fade-leave-to {
  opacity: 0;
  transform: translateX(-50%) translateY(20px);
}
</style> 